<?php
/**
 * Loader
 * 
 * @category	Loader
 * @package		dayscript.loader
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.0
 * @version		$Revision: 0 $
 * @filesource	
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-08-13 $
 */

namespace dayscript\loader;

/**
 * Loader
 * 
 * General auto loader 
 * 
 * @category	Loader
 * @package		dayscript.loader
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.0
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		\dayscript\loader\LoaderManager
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-09-01 $
 */
class Loader	{
	/**
	 * Loader name
	 * @var string
	 */
	protected $name;
	/**
	 * Libraries root directory with a trailing DIRECTORY_SEPARATOR.
	 * @var string
	 */
	protected $libraryDir;
	/**
	 * Library file's extension
	 * @var string
	 */
	protected $extension;
	/**
	 * Loader constructor
	 * @param string $name Loader identifier
	 * @param string $libraryDir Directory where the libraries are placed, with a trailing DIRECTORY_SEPARATOR.
	 * @param string $extension Library file's extension
	 * @return Loader
	 */
	public function __construct( $name, $libraryDir = null, $extension = '.class.php' )	{
		$this->name = $name;
		$this->libraryDir = $libraryDir;
		$this->extension = $extension;
		\dayscript\loader\LoaderManager::addLoader( $this );
	}
	/**
	 * Returns actual Loader identifier
	 * @return string $name
	 */
	public function getName( )	{
		return $this->name;
	}
	/**
	 * Returns actual Library directory
	 * @return string $libraryDir
	 */
	public function getLibraryDir( )	{
		return $this->libraryDir;
	}
	/**
	 * Whether or not a library can be loader by this Loader
	 * I recomend not to use this function in production for eficient purposes.
	 * @return boolean $testOK
	 * @param string $className
	 */
	public function canLoadClass( $className )	{
		return \file_exists( $this->libraryDir . $className . $this->extension );
	}
	/**
	 * Loads a class 
	 * I recomend to try to avoid any validation like \file_exists(...) or \class_exists(...) in this function for eficient purposes.
	 * @param string $className
	 */
	public function loadClass( $className )	{
		$path = $this->libraryDir . \str_replace( '\\', \DIRECTORY_SEPARATOR, $className ) . $this->extension;
		if( \file_exists( $path ) )
			require( $path );
	}
}